package cn.wolfcode.controller;


import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.domain.Notice;
import cn.wolfcode.enums.NoticeLevelEnum;
import cn.wolfcode.qo.NoticeQueryObject;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequiredPermission;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private INoticeService noticeService;
    @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") NoticeQueryObject qo){
        //设置管理员查询条件
        Employee employee = UserContext.getCurrentUser();
        qo.setUser(employee);
        PageInfo<Notice> pageInfo = noticeService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("values", NoticeLevelEnum.values());
        model.addAttribute("employee", employee);
        return "notice/list";
    }

    @RequestMapping("/delete")
    @RequiredPermission(name="公告通知删除",expression ="notice:delete")
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            noticeService.delete(id);
        }
        return new JsonResult();
    }


    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name="公告通知保存",expression ="notice:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Notice notice){
        if (notice.getId() != null) {
            noticeService.update(notice);
        }else {
            noticeService.save(notice);
        }
        return new JsonResult();
    }
    @RequestMapping("/changeStatus")
    @RequiredPermission(name="公告发布/取消",expression ="notice:changeStatus")
    @ResponseBody
    public JsonResult changeStatus(Long id,String status){
       boolean sta = Boolean.valueOf(status);
        noticeService.changeStatus(id,sta);
        return new JsonResult();
    }

    @RequestMapping("/show")
    public String show(Long id,Model model){
        Notice notice = noticeService.show(id);
        model.addAttribute("notice",notice);
        return "notice/show";
    }
}
